/******************************************************************************
 * @brief    命令管理器
 * Change Logs:
 * Date           Author         Email                   Notes
 * 2024-09-13      子恒     3393018959@qq.com      Initial version
 ******************************************************************************/

#ifndef __B_CMD_H__
#define __B_CMD_H__

#include "ringbuffer.h"
#include "string.h"
#include "stdint.h"

// 接收队列缓存大小
#define B_CMD_RING_MAX 32

// 一行命令缓存大小
#define B_CMD_LINE_CHAR_MAX 64

// 最大命令参数个数
#define B_CMD_ARGS_NUM_MAX 15u

// 字串分割符
#define B_CMD_SPLIT_STR " "

// 命令字符串结束分隔符
#define B_CMD_END_STR "\r\n"

// 执行成功响应输出
#define B_CMD_EXE_SUCESS_STR "CMD PASS!\r\n"

// 执行失败响应输出
#define B_CMD_EXE_FAIL_STR "CMD FAIL!\r\n"

// 子串日志使能
#define B_CMD_VAR_PRINT_EN 1

// 命令回调函数执行成功返回值
#define B_CMD_RET_PASS 0

// 命令回调函数执行失败返回值
#define B_CMD_RET_FAIL !B_CMD_RET_PASS

#define B_ARRY_SIZE(x) ((sizeof(x))/(sizeof(x[0])))

#if (B_CMD_RING_MAX & (B_CMD_RING_MAX - 1)) != 0
#error "B_CMD_RING_MAX is pow 2"
#endif

/* 输出字符回调函数 */
typedef void b_cmd_out_char_cb_t(char ch);

/* 命令回调函数 */
typedef unsigned char b_cmd_cb_t(char *argv[],unsigned char argc);

typedef struct
{
    const char *cmd_str;
    const char *cmd_help;
    unsigned short hash;
    unsigned char hash_argc;
    b_cmd_cb_t *cb;  
} b_cmd_t;

typedef struct
{
    b_cmd_t *cmds;  
    char line[B_CMD_LINE_CHAR_MAX]; 
    struct{
        unsigned short rsv          : 1;
        unsigned short cmds_len     : 15;
    }info;
    b_cmd_out_char_cb_t *pf_outchar; 
} b_cmd_mgr_t;


extern void b_cmd_put_str(const char *const str);
extern unsigned char cmd_ring_puts(unsigned char *pch,unsigned int len);
extern void cmd_mgr_init(b_cmd_t *cmds,unsigned char cmds_len, b_cmd_out_char_cb_t * putc_cb);
extern void b_cmd_parse(void);

#endif
